package com.lmx.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lmx.project.common.BaseResponse;
import com.lmx.project.common.ErrorCode;
import com.lmx.project.common.ResultUtils;
import com.lmx.project.exception.BusinessException;
import com.lmx.project.model.dto.category.CategoryRequestUpdate;
import com.lmx.project.model.dto.compition.CommpitionAddRequest;
import com.lmx.project.model.entity.Commpition;
import com.lmx.project.model.entity.Team;
import com.lmx.project.service.CommpitionService;
import com.lmx.project.service.TeamService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lmx
 * 本类负责有关竞赛名称的增删改查
 */
@RestController
@RequestMapping("/compition")
public class CompitionController {
    @Resource
    private CommpitionService commpitionService;

    @Resource
    private TeamService teamService;

    /**
     * 增加竞赛
     * TODO 管理员
     */
    @PostMapping
    public BaseResponse<Long> addCompition(@RequestBody CommpitionAddRequest commpitionAddRequest) {
        if (commpitionAddRequest.getName() == null || commpitionAddRequest.getName().equals("")) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "竞赛名称不能为空");
        }
        Commpition commpition = new Commpition();
        BeanUtils.copyProperties(commpitionAddRequest, commpition);
        boolean save = commpitionService.save(commpition);
        if (save) {
            return ResultUtils.success(commpition.getId());
        } else {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "请联系管理员或稍后重试");
        }

    }

    /**
     * 删除竞赛
     * TODO 管理员
     */
    @DeleteMapping
    public BaseResponse<Boolean> deleteCompition(Long id) {
        if (id == null || id == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

//        先查询团队中是否有
        LambdaQueryWrapper<Team> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Team::getCompitionid, id);
        long count = teamService.count(queryWrapper);
        if (count != 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请先删除该竞赛下所有团队");
        }
        boolean b = commpitionService.removeById(id);
        if (b) {
            return ResultUtils.success(b);
        } else {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "查无此数据");
        }

    }

    /**
     * 修改竞赛
     * TODO 管理员
     */
    @PutMapping
    public BaseResponse<Boolean> UpdateCompition(@RequestBody CategoryRequestUpdate categoryRequestUpdate) {
        if (categoryRequestUpdate.getId() == null || categoryRequestUpdate.getId() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id不能为空");
        }
        if (categoryRequestUpdate.getName() == null || categoryRequestUpdate.getName().equals("")) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "竞赛名称不能为空");
        }
        Commpition commpition = new Commpition();
        BeanUtils.copyProperties(categoryRequestUpdate, commpition);
        boolean save = commpitionService.updateById(commpition);
        if (save) {
            return ResultUtils.success(save);
        } else {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查无该数据");
        }

    }

    /**
     * 获取所有的竞赛列表
     */
    @GetMapping
    public BaseResponse<List<Commpition>> getCompition() {
        List<Commpition> list = commpitionService.list();

        return ResultUtils.success(list);

    }

}
